package cn.coder.mybatis.javamail.service;

import cn.coder.mybatis.javamail.model.AuthenticMail;
import cn.coder.mybatis.javamail.model.SendEmail;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import java.util.Date;
import java.util.Properties;

/**
 * Created by threegrand2 on 15-1-7.
 * 简单邮件发送器
 * 不带发送附件的
 */
public class SimpleSendMailService {
    /**
     * 使用文本格式发送邮件
     * @param sendEmail
     * @return
     */
   public boolean sendTextMail(SendEmail sendEmail){
       AuthenticMail authenticator = null;
       Properties pro = sendEmail.getMailProperties();
       if(sendEmail.isValidate()){
            authenticator = new AuthenticMail(sendEmail.getUserName(),sendEmail.getUserPass());
       }
       Session sendEmailSession  = Session.getDefaultInstance(pro,authenticator);

       // Ctrl + Alt + T  生成程序结构， 可以自动生成try catch 块
       try {
           Message message = new MimeMessage(sendEmailSession);
           //创建邮件的发送地址和接受地址
           Address from = new InternetAddress(sendEmail.getFromAddress());
           Address to = new InternetAddress(sendEmail.getToAddress());
           //设置发送接受的消息设置
           message.setFrom(from);                             //设置邮件的发送和接受地址
           message.setRecipient(Message.RecipientType.TO, to);

           message.setSubject(sendEmail.getSubject());        //设置邮件内容的主题
           String mailContent = sendEmail.getContent();
           message.setText(mailContent);                      //设置邮件消息的内容
           message.setSentDate(new Date());                   //设置邮件的发送日期

           //发送消息
           Transport.send(message);
            return true;
           } catch (MessagingException e) {
               e.printStackTrace();

       }
       return false;
   }

    /**
     * 以HTML格式发送邮件
     * @param sendEmail
     * @return
     */
    public static  boolean sendHtmlMail(SendEmail sendEmail){
        AuthenticMail authenticMail = null;
        Properties properties = sendEmail.getMailProperties();
        if(sendEmail.isValidate()){
            authenticMail = new AuthenticMail(sendEmail.getUserName(),sendEmail.getUserPass());
        }
        try {
            Session  sendMailSession = Session.getDefaultInstance(properties,authenticMail);
            Message message = new MimeMessage(sendMailSession);
            Address from = new InternetAddress(sendEmail.getFromAddress());
            Address to = new InternetAddress(sendEmail.getToAddress());
            message.setFrom(from);
            message.setRecipient(Message.RecipientType.TO,to);

            //设置邮件消息的主题
            message.setSubject(sendEmail.getSubject());
            String content = sendEmail.getContent();
            message.setSentDate(new Date());

            //MimeMultipart 是一个容器类，包含一个MimeBodyPart的对象
            Multipart mainPart = new MimeMultipart();

            //创建一个包含HTMl的MimeBodyPart的对象
            BodyPart htmlBody = new MimeBodyPart();
            htmlBody.setContent(content,"text/html;charset=utf-8");
            mainPart.addBodyPart(htmlBody);
            message.setContent(mainPart);

            Transport.send(message);                            //发送邮件

        } catch (Exception e) {
            e.printStackTrace();
        }

        return  false;
    }
}
